<?php
/**
 * 简历解析 Logic
 */

namespace app\common\logic\package;

use think\Exception;

/**
 *  压缩包
 * Class ZipArchiveLogic
 * @package app\common\logic
 */
class RarArchiveLogic extends BaseArchiveLogic {


    public function __construct()
    {
        parent::__construct();
        $this->setArchive();
    }
    public function setArchive(){
        $this->class = \RarArchive::class;
    }
    public function open($file){
        $this->class = $this->class::open($file);
        return $this;
    }
    /**
     *
     * @param string $dir
     * @return $this|bool
     */
    public function extractTo($dir,$file = null){
        $this->mkdir($dir);
//        $this->class->getEntries($dir);

        $entries = $this->class->getEntries();

        foreach ($entries as $k=>$entry) {
            try{
                $path_info = pathinfo($entry->getName());
                if(empty($path_info['extension']) || !in_array($path_info['extension'],['pdf','docx'])){
                    continue;
                }
                $file_name = uniqid().$k.'.'.($path_info['extension']??'');

                $entry->extract(false,$dir.'/'.$file_name);
            }catch (Exception $e){

            }
        }

        return $this;
    }

}